﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
public partial class CallRoute_Customer_Coverage_Assignment : PageBase
{
    CustomerRouteManager manager = new CustomerRouteManager();
    int OlduserID = 0;
    decimal NewuserID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Int32.TryParse(ddlOldUser.SelectedValue, out OlduserID);
        decimal.TryParse(ddlNewUser.SelectedValue,out NewuserID);
        if (this.txtHidOrg.Value == "")
        {
            this.txtHidOrg.Value = OrgHidValue;
            this.txtOrg.Text = OrgShowValue;
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, txtHidOrg.Value, this.ddlNewUser);
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, txtHidOrg.Value, this.ddlOldUser);
        }
        if (!IsPostBack)
        {
            DataBind();
            BindOldUser();
            BindGrid();
            
        }

    }

    private void BindOldUser()
    {
        if (txtHidOrg.Value != "")
        {
            DataTable dt = new MobileUserManager().Load(MyStateManager.DomainID, txtHidOrg.Value);
            ddlOldUser.DataTextField = "CODENAME";
            ddlOldUser.DataValueField = "ID";
            ddlOldUser.DataSource = dt;
            ddlOldUser.DataBind();
            ddlOldUser.Items.Insert(0, new ListItem("--请选择--", ""));

            DataTable dtt = new MobileUserManager().Load(MyStateManager.DomainID, txtHidOrg.Value);
            ddlNewUser.DataTextField = "CODENAME";
            ddlNewUser.DataValueField = "ID";
            ddlNewUser.DataSource = dtt;
            ddlNewUser.DataBind();
            ddlNewUser.Items.Insert(0, new ListItem("--请选择--", ""));
        }
    }
    protected void ddlOldUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            if (ddlNewUser.SelectedValue != string.Empty && ddlOldUser.SelectedValue != string.Empty)
            {
                for (int i = 0; i < gvChannelType.Rows.Count; i++)
                {
                    try
                    {
                        CheckBox cbox = (CheckBox)gvChannelType.Rows[i].FindControl("cbxIsSelect");
                        if (cbox != null && cbox.Checked == true)
                        {
                            decimal oldUserid = decimal.Parse(ddlOldUser.SelectedValue);
                            decimal newUserid = decimal.Parse(ddlNewUser.SelectedValue);
                            decimal customerid = decimal.Parse(gvChannelType.DataKeys[i].Values[0].ToString());
                            decimal rcid = decimal.Parse(gvChannelType.DataKeys[i].Values[1].ToString());

                            CustomerRouteManager.TransferCustomerToAnotherUser(rcid, oldUserid, customerid, newUserid);
                        }
                    }
                    catch (Exception ex)
                    {
                        sb.Append(ex.Message);
                        sb.Append("\\n");
                    }
                }
                if (sb.ToString().Length > 0)
                {
                    throw new Exception(sb.ToString());
                }
            }
            BindGrid();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message +"已经存在目标用户覆盖列表当中!");
        }
    }

    private void BindGrid()
    {
        int userID = 0;
        int.TryParse(ddlOldUser.SelectedValue, out userID);
        DataSet ds = CustomerRouteManager.SelectUserCustomer(userID, this.MyStateManager.DomainID,this.MyStateManager.OrgID);
        if (ds != null && ds.Tables.Count > 0)
        {
            gvChannelType.DataSource = ds.Tables[0];
            gvChannelType.DataBind();
        }
    }
    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        BindOldUser();
    }
}
